<?php


namespace app\model;

use think\facade\Db;
use think\response\Json;

/** 运营方管理
 * Class PlatModel
 * @package app\model
 */
class OperateModel
{
    private static $_baseTable = 'eb_admin'; // 管理员表
    private static $_storeTable = 'eb_store'; // 商户表
    private static $_schoolTable = 'eb_school'; // 学校表
    private static $_schoolStoreTable = 'eb_school_store'; // 学校商户关联表
    private static $_orderTable = 'eb_order'; // 订单表

    private static $_user = 'eb_user'; // 用户表
    private static $_map = [['type', '=', 3]]; // 默认查询条件
    /**运营方列表
     * @param $input
     * @param $token
     * @return \think\response\Json
     */
    public static function superList($input, $token)
    {
        try {
            // 检测当前管理员状态
            $rtn = AdminModel::getInfo($token, [1], [1]);
            if ($rtn instanceof Json) return $rtn;
            // 获取列表
            $table = Db::table(self::$_baseTable)->alias('o')->join([self::$_baseTable => 'p'], 'o.pid=p.id');
            $table->field('o.id, o.account, o.name, o.phone, o.status, o.bank_name,o.bank_no,o.delivery_time,o.send_delaytime,FROM_UNIXTIME(o.create_time, "%Y-%m-%d %H:%i:%s") AS create_time, p.name as plat_name');
            self::$_map = [['o.type', '=', 3]];
            if ($input['name'] !== '') self::$_map[] = ['o.name', 'like', "%{$input['name']}%"];
            if ($input['pid'] !== '') self::$_map[] = ['o.pid', '=', $input['pid']];
            $table->where(self::$_map);
            $count = $table->count();
            if ($count == 0 ) return jsonSuccess('查询成功', ['count'=> $count, 'list' => []]);
            $list = $table->order('o.create_time', 'desc')->page($input['page'], $input['limit'])->select();
            return jsonSuccess('查询成功', ['count'=> $count, 'list' => $list]);
        } catch (\Exception $e) {
            return jsonFail('服务内部错误~');
        }
    }

    /**平台方获取运营方列表
     * @param $input
     * @param $token
     * @return \think\response\Json
     */
    public static function platList($input, $token)
    {
        try {
            // 检测当前管理员状态
            $rtn = AdminModel::getInfo($token, [1], [2]);
            if ($rtn instanceof Json) return $rtn;
            // 获取列表
            $table = Db::table(self::$_baseTable);
            $table->field('id, account, name, phone, status,bank_name,bank_no,delivery_time,send_delaytime,FROM_UNIXTIME(create_time, "%Y-%m-%d %H:%i:%s") AS create_time');
            self::$_map[] = ['pid', '=', $rtn['id']];
            if ($input['name'] !== '') self::$_map[] = ['name', 'like', "%{$input['name']}%"];
            $table->where(self::$_map);
            $count = $table->count();
            if ($count == 0 ) return jsonSuccess('查询成功', ['count'=> $count, 'list' => []]);
            $list = $table->order('create_time', 'desc')->page($input['page'], $input['limit'])->select();
            return jsonSuccess('查询成功', ['count'=> $count, 'list' => $list]);
        } catch (\Exception $e) {
            return jsonFail('服务内部错误~');
        }
    }

    /**获取简单列表
     * @param $token
     * @return \think\response\Json
     */
    public static function simpleList($token)
    {
        try {
            // 检测当前管理员状态
            $rtn = AdminModel::getInfo($token);
            if ($rtn instanceof Json) return $rtn;
            // 获取列表
            self::$_map[] = ['status', '=',1];
            $info = Db::table(self::$_baseTable)->field('id,name,pid')->where(self::$_map)->select();
            return jsonSuccess('获取成功', $info);
        } catch (\Exception $e) {
            return jsonFail('服务内部错误~');
        }
    }

    /** 添加运营方
     * @param $input
     * @param $token
     * @return \think\response\Json
     */
    public static function add($input, $token)
    {
        try {
            // 检测当前管理员状态
            $rtn = AdminModel::getInfo($token, [1], [1,2]);
            if ($rtn instanceof Json) return $rtn;
            // 判断是否已经添加
            $info = Db::table(self::$_baseTable)->field('id')->where('account', $input['account'])->find();
            if ($info) return jsonFail('账号已存在，不能添加');
            // 添加运营方
            unset($input['repwd']);
            $input['pwd'] = sha1($input['pwd']);
            $input['type'] = 3;
            $input['ptype'] = 2;
            $input['create_time'] = time();
            if ($rtn['type'] == 2) $input['pid'] = $rtn['id'];
            $rtn = Db::table(self::$_baseTable)->insertGetId($input);
            if ($rtn >= 1) return jsonSuccess('添加成功');
            return jsonFail('添加失败');
        } catch (\Exception $e) {
            return jsonFail('服务内部错误~');
        }
    }

    /** 删除运营方
     * @param $input
     * @param $token
     * @return \think\response\Json
     */
    public static function delete($input, $token)
    {
        try {
            // 检测当前管理员状态
            $rtn = AdminModel::getInfo($token, [1], [1,2]);
            if ($rtn instanceof Json) return $rtn;
            // 删除运营方
            $rtn = Db::table(self::$_baseTable)->where(self::$_map)->delete($input['id']);
            if ($rtn == 1) return jsonSuccess('删除成功');
            return jsonFail('删除失败');
        } catch (\Exception $e) {
            return jsonFail('服务内部错误~');
        }
    }

    /** 获取单个信息
     * @param $input
     * @param $token
     * @return \think\response\Json
     */
    public static function only($input, $token)
    {
        try {
            // 检测当前管理员状态
            $rtn = AdminModel::getInfo($token, [1], [1,2]);
            if ($rtn instanceof Json) return $rtn;
            // 获取单个信息
            self::$_map[] = ['id', '=', $input['id']];
            $rtn = Db::table(self::$_baseTable)->field('id,account,name,phone,status,pid,bank_no,bank_name,delivery_time,send_delaytime')->where(self::$_map)->find();
            if ($rtn) return jsonSuccess('获取成功', $rtn);
            return jsonFail('未找到信息');
        } catch (\Exception $e) {
            return jsonFail('服务内部错误~');
        }
    }

    /** 编辑运营方
     * @param $input
     * @param $token
     * @return \think\response\Json
     */
    public static function edit($input, $token)
    {
        try {
            // 检测当前管理员状态
            $rtn = AdminModel::getInfo($token, [1], [1,2]);
            if ($rtn instanceof Json) return $rtn;
            // 编辑运营方
            self::$_map[] = ['id', '=', $input['id']];
            unset($input['id']);
            unset($input['pid']);
            $rtn = Db::table(self::$_baseTable)->where(self::$_map)->update($input);
            if ($rtn) return jsonSuccess('编辑成功');
            return jsonFail('编辑失败');
        } catch (\Exception $e) {
            var_dump($e->getMessage());
            return jsonFail('服务内部错误~');
        }
    }

    /** 修改密码
     * @param $input
     * @param $token
     * @return \think\response\Json
     */
    public static function changePwd($input, $token)
    {
        try {
            // 检测当前管理员状态
            $rtn = AdminModel::getInfo($token, [1], [1,2]);
            if ($rtn instanceof Json) return $rtn;
            // 获取单个信息
            unset($input['chkpwd']);
            $input['pwd'] = sha1($input['pwd']);
            self::$_map[] = ['id', '=', $input['id']];
            $rtn = Db::table(self::$_baseTable)->where(self::$_map)->update($input);
            if ($rtn) return jsonSuccess('修改成功');
            return jsonFail('修改失败');
        } catch (\Exception $e) {
            return jsonFail('服务内部错误~');
        }
    }

    /** 获取商户简单列表
     * @param $token
     * @return \think\response\Json
     */
    public static function getStoreList($token)
    {
        try {
            // 检测当前管理员状态
            $rtn = AdminModel::getInfo($token, [1], [3]);
            if ($rtn instanceof Json) return $rtn;
            // 获取当前运营商下的学校
            $info = Db::table(self::$_schoolTable)->alias('sc')
                ->join([self::$_schoolStoreTable => 'scs'], 'sc.id = scs.school_id')
                ->join([self::$_storeTable => 's'], 's.id = scs.store_id')
                ->where('sc.operate_id', $rtn['id'])
                ->field('s.id,s.shop_name')
                ->group('scs.store_id')
                ->select()->toArray();
            return jsonSuccess('获取成功', ['list' => $info]);
        } catch (\Exception $e) {
            return jsonFail('服务内部错误~');
        }
    }

    /** 获取超时配送时长 延迟派单时间
     * @param $token
     * @return \think\response\Json
     */
    public static function getConf($token)
    {
        try {
            // 检测当前管理员状态
            $rtn = AdminModel::getInfo($token, [1], [3]);
            if ($rtn instanceof Json) return $rtn;
            // 获取当前运营商下的学校
            $info = Db::table(self::$_baseTable)
                ->field('id,delivery_time,send_delaytime')
                ->where('id', $rtn['id'])
                ->find();
            return jsonSuccess('获取成功',  $info);
        } catch (\Exception $e) {
            return jsonFail('服务内部错误~');
        }
    }

    /** 编辑运营方配置
     * @param $input
     * @param $token
     * @return \think\response\Json
     */
    public static function confEdit($input, $token)
    {
        try {
            // 检测当前管理员状态
            $rtn = AdminModel::getInfo($token, [1], [3]);
            if ($rtn instanceof Json) return $rtn;
            // 编辑运营方
            $rtn = Db::table(self::$_baseTable)->update($input);
            if ($rtn) return jsonSuccess('编辑成功');
            return jsonFail('编辑失败');
        } catch (\Exception $e) {
            return jsonFail('服务内部错误~');
        }
    }


    /** 获取超时配送时长 延迟派单时间
     * @param $input
     * @param $token
     * @return \think\response\Json
     */
    public static function getSenderList($input, $token)
    {
        try {
            // 检测当前管理员状态
            $rtn = AdminModel::getInfo($token, [1], [3]);
            if ($rtn instanceof Json) return $rtn;
            // 获取当前运营商下的骑手
            $map[] = ['s.operate_id', '=', $rtn['id']];
            if ($input['name'] !== '') $map[] = ['u.name', 'LIKE', "%{$input['name']}%"];
            $list = Db::table(self::$_schoolTable)->alias('s')
                ->join([self::$_user => 'u'], 'u.school_id = s.id')
                ->field('u.name, u.phone,u.id')
                ->where($map)
                ->select()->toArray();
            return jsonSuccess('获取成功',  $list);
        } catch (\Exception $e) {
            return jsonFail('服务内部错误~');
        }
    }

    /** 获取超时骑手订单
     * @param $input
     * @param $token
     * @return \think\response\Json
     */
    public static function getOvertimeList($input, $token)
    {
        try {
            // 检测当前管理员状态
            $rtn = AdminModel::getInfo($token, [1], [3]);
            if ($rtn instanceof Json) return $rtn;
            // 获取当前运营商下的骑手
            $map[] = ['o.operate_id', '=', $rtn['id']];
            $map[] = ['is_overtime', '=', 1];
            if ($input['send_id'] !== '') $map[] = ['o.send_id', '=', $input['send_id']];
            if ($input['start'] !== '') $map[] = ['o.create_time', '>=', strtotime($input['start'])];
            if ($input['end'] !== '') $map[] = ['o.create_time', '<=', strtotime($input['end'])];
            $table = Db::table(self::$_orderTable)->alias('o')
                ->join([self::$_user => 'u'], 'u.id = o.send_id')
                ->field('u.id,u.username,u.name,u.phone,u.type,u.gender,group_concat(o.id) as order_ids')
                ->where($map)
                ->group('u.id');
            $count = $table->count();
            if ($count == 0 ) return jsonSuccess('查询成功', ['count'=> $count, 'list' => []]);
            $list = $table->group('u.id')->order('o.create_time', 'desc')->page($input['page'], $input['limit'])->select()->toArray();
            return jsonSuccess('查询成功', ['count'=> $count, 'list' => $list]);
        } catch (\Exception $e) {
            return jsonFail('服务内部错误~');
        }
    }

    /** 获取超时骑手订单详情
     * @param $input
     * @param $token
     * @return \think\response\Json
     */
    public static function overtimeDetail($input, $token)
    {
        try {
            // 检测当前管理员状态
            $rtn = AdminModel::getInfo($token, [1], [3]);
            if ($rtn instanceof Json) return $rtn;
            // 获取当前运营商下的骑手
            $map[] = ['operate_id', '=', $rtn['id']];
            $map[] = ['id', 'in', $input['order_ids']];
            $table = Db::table(self::$_orderTable)
                ->field('id,order_no,receipt_time,send_time,arrive_time,over_time,create_time')
                ->where($map);
            $count = $table->count();
            if ($count == 0 ) return jsonSuccess('查询成功', ['count'=> $count, 'list' => []]);
            $list = $table->order('create_time', 'desc')->page($input['page'], $input['limit'])->select()->toArray();
            return jsonSuccess('查询成功', ['count'=> $count, 'list' => $list]);
        } catch (\Exception $e) {
            return jsonFail('服务内部错误~');
        }
    }
}